package com.ilop.sthome.ui.adapter.device;

import android.content.Context;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.ilop.sthome.data.bean.DeviceHistoryBean;
import com.siterwell.familywellplus.R;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author skygge
 * @date 2020-01-10.
 * GitHub：javofxu@github.com
 * email：skygge@yeah.net
 * description：设备历史记录
 */
public class DeviceHistoryAdapter extends RecyclerView.Adapter<DeviceHistoryAdapter.ItemHolder> {

    private Context mContext;
    private List<DeviceHistoryBean> mList;
    private String[] mMonth;

    public DeviceHistoryAdapter(Context mContext) {
        this.mContext = mContext;
        mMonth = new String[]{mContext.getString(R.string.month_01), mContext.getString(R.string.month_02),
                mContext.getString(R.string.month_03), mContext.getString(R.string.month_04),
                mContext.getString(R.string.month_05), mContext.getString(R.string.month_06),
                mContext.getString(R.string.month_07), mContext.getString(R.string.month_08),
                mContext.getString(R.string.month_09), mContext.getString(R.string.month_10),
                mContext.getString(R.string.month_11), mContext.getString(R.string.month_12)};
    }

    public void setList(List<DeviceHistoryBean> mList) {
        this.mList = mList;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public ItemHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View mView = LayoutInflater.from(mContext).inflate(R.layout.item_device_history, viewGroup, false);
        return new ItemHolder(mView);
    }

    @Override
    public void onBindViewHolder(@NonNull ItemHolder itemHolder, int i) {
        DeviceHistoryBean history = mList.get(i);
        String month = mMonth[history.getMonth()-1];
        String day = String.valueOf(history.getDay());
        itemHolder.mMonth.setText(month);
        itemHolder.mDay.setText(day);
        itemHolder.mMum.setText(String.valueOf(history.getNumber()));
        if (history.getList()!=null){
            HistoryDetailAdapter mAdapter = new HistoryDetailAdapter(mContext);
            mAdapter.setList(history.getList());
            itemHolder.mDetail.setLayoutManager(new LinearLayoutManager(mContext));
            itemHolder.mDetail.setAdapter(mAdapter);
        }else if (history.getSubList()!=null){
            SubHistoryDetailAdapter mAdapter = new SubHistoryDetailAdapter(mContext);
            mAdapter.setList(history.getSubList());
            itemHolder.mDetail.setLayoutManager(new LinearLayoutManager(mContext));
            itemHolder.mDetail.setAdapter(mAdapter);
        }
    }

    @Override
    public int getItemCount() {
        return mList == null ? 0 : mList.size();
    }

    static class ItemHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.item_history_day)
        TextView mDay;
        @BindView(R.id.item_history_month)
        TextView mMonth;
        @BindView(R.id.item_history_mum)
        TextView mMum;
        @BindView(R.id.item_history_detail)
        RecyclerView mDetail;

        ItemHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
